package sunyu.tools.redis;

import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import sunyu.tools.redis.serializer.Serializer;

import javax.annotation.Resource;
import java.util.Map;

@Component
public class RedisTools {

    //这里可以选择序列化工具，fstSerializer比较快，兼容jdk；jdkSerializer速度比较慢
    //@Resource(name = "jdkSerializer")
    @Resource(name = "fstSerializer")
    private Serializer serializer;
    @Resource(name = "jedisPool")
    private JedisPool jedisPool;

    private Jedis getJedis() {
        return jedisPool.getResource();
    }

    /**
     * 存入一个字符串
     *
     * @param key   键
     * @param value 值
     *
     * @return 状态(ok)
     */
    public String set(String key, String value) {
        Jedis jedis = getJedis();
        String status = null;
        try {
            status = jedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return status;
    }

    /**
     * 存入一个带生存时间的字符串
     *
     * @param key
     * @param seconds 生存时间(秒)
     * @param value
     *
     * @return
     */
    public String setex(String key, int seconds, String value) {
        Jedis jedis = getJedis();
        String status = null;
        try {
            status = jedis.setex(key, seconds, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return status;
    }

    /**
     * 存入一个对象
     *
     * @param key
     * @param value
     *
     * @return
     */
    public String set(String key, Object value) {
        Jedis jedis = getJedis();
        String status = null;
        try {
            status = jedis.set(serializer.keyToBytes(key), serializer.valueToBytes(value));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return status;
    }

    /**
     * 存入一个带生存时间的对象
     *
     * @param key
     * @param seconds 生存时间(秒)
     * @param value
     *
     * @return
     */
    public String setex(String key, int seconds, Object value) {
        Jedis jedis = getJedis();
        String status = null;
        try {
            status = jedis.setex(serializer.keyToBytes(key), seconds, serializer.valueToBytes(value));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return status;
    }

    /**
     * 获得一个字符串
     *
     * @param key
     *
     * @return
     */
    public String get(String key) {
        Jedis jedis = getJedis();
        String value = null;
        try {
            value = jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return value;
    }

    /**
     * 获得一个对象
     *
     * @param key
     *
     * @return
     */
    public Object getObject(String key) {
        Jedis jedis = getJedis();
        byte[] value = null;
        try {
            value = jedis.get(serializer.keyToBytes(key));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return serializer.valueFromBytes(value);
    }

    /**
     * 删除一个字符串
     *
     * @param key
     *
     * @return
     */
    public Long del(String key) {
        Jedis jedis = getJedis();
        Long l = null;
        try {
            l = jedis.del(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return l;
    }

    /**
     * 删除一个对象
     *
     * @param key
     *
     * @return
     */
    public Long delObject(String key) {
        Jedis jedis = getJedis();
        Long l = null;
        try {
            l = jedis.del(serializer.keyToBytes(key));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return l;
    }

    /**
     * 更改库中字符串的生存时间
     *
     * @param key
     * @param seconds 生存时间(秒)
     *
     * @return
     */
    public Long expire(String key, int seconds) {
        Jedis jedis = getJedis();
        Long l = null;
        try {
            l = jedis.expire(key, seconds);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return l;
    }

    /**
     * 更改库中对象的生存时间
     *
     * @param key
     * @param seconds 生存时间(秒)
     *
     * @return
     */
    public Long expireObject(String key, int seconds) {
        Jedis jedis = getJedis();
        Long l = null;
        try {
            l = jedis.expire(serializer.keyToBytes(key), seconds);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return l;
    }

    /**
     * 获得散列中的所有
     *
     * @param hKey
     *
     * @return
     */
    public Map<String, String> hgetAll(String hKey) {
        Jedis jedis = getJedis();
        Map<String, String> hashAll = null;
        try {
            hashAll = jedis.hgetAll(hKey);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return hashAll;
    }

    /**
     * 获得散列里的某一项的值
     *
     * @param key
     * @param field
     *
     * @return
     */
    public String hget(String key, String field) {
        Jedis jedis = getJedis();
        String result = null;
        try {
            result = jedis.hget(key, field);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return result;
    }

}
